<template>
  <view class="setting">

    <u-cell-group :border="false" style="background-color:#fff;border-radius: 20rpx;margin-bottom: 20rpx;"  v-for="(item,index) in navList" :key="index">
      <u-cell
              :title="item.title" :value="item.right"
              :rightIconStyle="{fontSize:'32rpx',color:'#373737'}"
              :isLink="true"
              size="large"
              :url="item.url"
              :border="false">
      </u-cell>
    </u-cell-group>

    <u-cell-group :border="false" style="background-color:#fff;border-radius: 20rpx;margin-bottom: 20rpx;">
      <u-cell
          @click="clearCache"
          title="清除缓存"
          :rightIconStyle="{fontSize:'32rpx',color:'#373737'}"
          :value="fileSizeString"
          :isLink="true"
          size="large"
          :border="false">
      </u-cell>
    </u-cell-group>

    <u-cell-group :border="false" style="background-color:#fff;border-radius: 20rpx;margin-bottom: 20rpx;">
      <u-cell
          @click="showLogOut = true"
          title="注销账号"
          :rightIconStyle="{fontSize:'32rpx',color:'#373737'}"
          :isLink="true"
          size="large"
          :border="false">
      </u-cell>
    </u-cell-group>

    <u-modal :show="showLogOut"
             :title-style="{ color: '#ffffff' }"
             :content-style="{ color: '#ffffff' }"
             cancel-color="#333333"
             :show-cancel-button="true"
             :mask-close-able="true"
             @confirm="delUser"
             @cancel="showLogOut = false"
             :closeOnClickOverlay="true"
             confirm-color="#e81f4b"
             content="确定注销吗？"
             confirm-text="确认"
             cancel-text="取消"
             title="提示"
    ></u-modal>
  </view>
</template>
<script>
import {gotoPage} from "../../utils/common";
import {Api} from "../../service/api";

export default {
  data() {
    return {
      showLogOut: false,
      userinfo: {},
      fileSizeString: null, // 缓存大小
      navList: [
        {
          title: '隐私协议',
          url: '/pages/Personal/setting/privacy',
        },
        {
          title: '用户协议',
          url: '/pages/Personal/setting/userAgreement',
        },
        {
          title: '消息设置',
          url: '/pages/Personal/setting/msgSet',
        },
        {
          title: '版本号',
          right: uni.getSystemInfoSync().appWgtVersion,
          url: '',
        },
        {
          title: 'APP备案信息',
          url: '/pages/Personal/setting/archivalInformation',
        },
      ]
    }
  },
  async onShow() {
    this.formatSize()
  },
  methods: {
    gotoPage,
    delUser(){
      // Api.logout().then(res=>{
        uni.clearStorageSync();
        uni.showToast({
          title: "注销成功",
          icon: "none"
        })
        setTimeout(()=>{
          uni.reLaunch({
            url: '/pages/benefit/index'
          })
        },1000)
      // })
    },
    // 清理缓存
    clearCache() {
      let that = this;
      let os = plus.os.name;
      if (os === 'Android') {
        let main = plus.android.runtimeMainActivity();
        let sdRoot = main.getCacheDir();
        let files = plus.android.invoke(sdRoot, "listFiles");
        let len = files.length;
        for (let i = 0; i < len; i++) {
          let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径
          plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
            if (entry.isDirectory) {
              entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录
                uni.showToast({
                  title: '缓存清理完成',
                  duration: 2000
                });
                that.formatSize(); // 重新计算缓存
              }, function(e) {
                console.log(e.message)
              });
            } else {
              entry.remove();
            }
          }, function(e) {
            console.log('文件路径读取失败')
          });
        }
      } else { // ios
        plus.cache.clear(function() {
          uni.showToast({
            title: '缓存清理完成',
            duration: 2000
          });
          that.formatSize();
        });
      }
    },
    formatSize() {
      // 获取缓存
      plus.cache.calculate((size) => {
        let sizeCache = parseInt(size);
        if (sizeCache === 0) {
          this.fileSizeString = "0B";
        } else if (sizeCache < 1024) {
          this.fileSizeString = sizeCache + "B";
        } else if (sizeCache < 1048576) {
          this.fileSizeString = (sizeCache / 1024).toFixed(2) + "KB";
        } else if (sizeCache < 1073741824) {
          this.fileSizeString = (sizeCache / 1048576).toFixed(2) + "MB";
        } else {
          this.fileSizeString = (sizeCache / 1073741824).toFixed(2) + "GB";
        }
      });
    }
  }
}
</script>

<style>
page{
  background: #f5f5f5;
}
</style>

<style scoped lang="scss">
.setting{
  padding: 30rpx 25rpx 0;

  .info{
    display: flex;
    align-items: center;
  }

  .logout_btn{
    font-weight: 400;
    width: 80%;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100rpx;
    text-align: center;
    color: #fff;
    background: #1ABAB0;
    border-radius: 60rpx;
    position: fixed;
    bottom: 100rpx;
    left: 50%;
    transform: translateX(-50%);
  }
}
</style>
